package com.juns.wechat; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Environment; import android.text.TextUtils; import com.baidu.frontia.FrontiaApplication; import com.easemob.chat.EMChat; import com.easemob.chat.EMChatManager; import com.easemob.chat.EMChatOptions; import com.easemob.chat.EMMessage; import com.easemob.chat.EMMessage.ChatType; import com.easemob.chat.OnMessageNotifyListener; import com.easemob.chat.OnNotificationClickListener; import com.juns.wechat.chat.ChatActivity; import com.juns.wechat.chat.VoiceCallActivity; public class App extends FrontiaApplication { private static Context _context; @Override public void onCreate() { super.onCreate(); _context = getApplicationContext(); initEMChat(); EMChat.getInstance().init(_context); EMChat.getInstance().setDebugMode(true); EMChat.getInstance().setAutoLogin(true); EMChatManager.getInstance().getChatOptions().setUseRoster(true); FrontiaApplication.initFrontiaApplication(this); // CrashHandler crashHandler = CrashHandler.getInstance();// 全局异常捕捉 // crashHandler.init(_context); } private void initEMChat() { int pid = android.os.Process.myPid(); String processAppName = getAppName(pid); if (processAppName == null || !processAppName.equalsIgnoreCase("com.juns.wechat")) { return; } EMChatOptions options = EMChatManager.getInstance().getChatOptions(); // 获取到EMChatOptions对象 // 设置自定义的文字提示 options.setNotifyText(new OnMessageNotifyListener() { @Override public String onNewMessageNotify(EMMessage message) { return "你的好友发来了一条消息哦"; } @Override public String onLatestMessageNotify(EMMessage message, int fromUsersNum, int messageNum) { return fromUsersNum + "个好友,发来了" + messageNum + "条消息"; } @Override public String onSetNotificationTitle(EMMessage arg0) { return null; } @Override public int onSetSmallIcon(EMMessage arg0) { return 0; } }); options.setOnNotificationClickListener(new OnNotificationClickListener() { @Override public Intent onNotificationClick(EMMessage message) { Intent intent = new Intent(_context, MainActivity.class); ChatType chatType = message.getChatType(); if (chatType == ChatType.Chat) { // 单聊信息 intent.putExtra("userId", message.getFrom()); intent.putExtra("chatType", ChatActivity.CHATTYPE_SINGLE); } else { // 群聊信息 // message.getTo()为群聊id intent.putExtra("groupId", message.getTo()); intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP); } return intent; } }); // IntentFilter callFilter = new // IntentFilter(EMChatManager.getInstance() // .getIncomingCallBroadcastAction()); // registerReceiver(new CallReceiver(), callFilter); } private class CallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 拨打方username String from = intent.getStringExtra("from"); // call type String type = intent.getStringExtra("type"); startActivity(new Intent(_context, VoiceCallActivity.class) .putExtra("username", from).putExtra("isComingCall", true)); } } private String getAppName(int pID) { String processName = null; ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE); List l = am.getRunningAppProcesses(); Iterator i = l.iterator(); PackageManager pm = this.getPackageManager(); while (i.hasNext()) { ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i .next()); try { if (info.pid == pID) { CharSequence c = pm.getApplicationLabel(pm .getApplicationInfo(info.processName, PackageManager.GET_META_DATA)); processName = info.processName; return processName; } } catch (Exception e) { } } return processName; } @Override public void onLowMemory() { super.onLowMemory(); try { deleteCacheDirFile(getHJYCacheDir(), true); } catch (IOException e) { e.printStackTrace(); } System.gc(); } public static Context getInstance() { return _context; } // 运用list来保存们每一个activity是关键 private List<Activity> mList = new LinkedList<Activity>(); private static App instance; // 构造方法 // 实例化一次 public synchronized static App getInstance2() { if (null == instance) { instance = new App(); } return instance; } // add Activity public void addActivity(Activity activity) { mList.add(activity); } // 关闭每一个list内的activity public void exit() { try { for (Activity activity : mList) { if (activity != null) activity.finish(); } } catch (Exception e) { e.printStackTrace(); } finally { System.exit(0); } } public static String getHJYCacheDir() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) return Environment.getExternalStorageDirectory().toString() + "/Health/Cache"; else return "/System/com.juns.Walk/Walk/Cache"; } public static String getHJYDownLoadDir() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) return Environment.getExternalStorageDirectory().toString() + "/Walk/Download"; else { return "/System/com.Juns.Walk/Walk/Download"; } } public static void deleteCacheDirFile(String filePath, boolean deleteThisPath) throws IOException { if (!TextUtils.isEmpty(filePath)) { File file = new File(filePath); if (file.isDirectory()) {// 处理目录 File files[] = file.listFiles(); for (int i = 0; i < files.length; i++) { deleteCacheDirFile(files[i].getAbsolutePath(), true); } } if (deleteThisPath) { if (!file.isDirectory()) {// 如果是文件,删除 file.delete(); } else {// 目录 if (file.listFiles().length == 0) {// 目录下没有文件或者目录,删除 file.delete(); } } } } } }